#!/bin/bash

# Copyright © 2016-2024 Jeffrey Cliff
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

HOW_SLOW_IS_SPC=59
BASEDIR=/home/themusicgod1
DBDIR=/home/themusicgod1/TDL

echo "initializing /tmp/totallymissingdomains..."
mv /tmp/totallymissingdomains /tmp/oldtotallymissingdomains 2>/dev/null
mv /tmp/missingdomains /tmp/oldmissingdomains 2>/dev/null
echo "OK [dont press enter yet]"

# 0. generate databases.db

# 1.
#sqlite3 database.db -line "select url from statuses;"  | grep url | awk -F' ' '{print $3}'| awk -F'/' '{print $2"@" $1}' | sort | uniq > users.txt
# get users script in aspublic

# 2. move users to eva

# 3.
cat users.txt | grep -iv '@.*@.*' > users_malformatted.txt

# 4.

cat users_malformatted.txt | awk -F' ' '{print "@"$1}' >> users2.txt
cat users.txt | grep -i '@.*@.*' >> users2.txt
mv users2.txt users.txt

echo "checking for lower/uppercase conflicts:"

for x in `cat users.txt`; do
check_user_for_caps.sh $x | awk -F'=' '{print $2}'; 
done;

echo "press enter to continue"
read

#what should we do when the domain name or the username is update?
# just grepping 'domain is' out should do the trick
for x in `cat users.txt`; do
#echo $x;
howFar.sh $x | grep -iv 'domain is' |grep -i update | grep -iv 'other = ""' | grep -iv 'insert' | grep -iv '@';  done;
#these i think can be just dropped in, no harm if they are dupes
echo "done. press enter or something";
read ;

echo "preprocessing domains for cf. . . "

echo "totally missing domains:"

file -E /tmp/totallymissingdomains >/dev/null
if [[ $? -eq "0" ]]; then
# if it exists
cat /tmp/totallymissingdomains | sort | uniq
fi

echo "done."
echo "press enter to continue"
read
echo "doing iscf part..."

file -E /tmp/missingdomains >/dev/null
if [[ $? -eq "0" ]]; then
# if it exists
for x in `cat /tmp/missingdomains | sort | uniq `; do echo $x `is_cloudflare $x` | sed -e "s/\^//" | sed -e "s/\\$//" > /tmp/cf_temp2 ; 
cat /tmp/cf_temp2 | grep " yes"  | awk -F' ' '{print "update instances set iscf=1 where name = \""$1"\";"}'
cat /tmp/cf_temp2 | grep " no"  | awk -F' ' '{print "update instances set iscf=0 where name = \""$1"\";"}'
done;
fi

echo "domains preprocessed. check /tmp/cf_temp2. press enter or something";
read ;

for x in `cat users.txt`; do echo $x; LINEME=`howFar.sh $x | grep -i insert `; echo $LINEME; sqlite3 ~/TDL/urls.db -line "$LINEME";  done
#what we should do next is do some kind of error checking instead of sleep here - print OK if success

#todo: make this dependent on max value
#or make it 100?
MAXLEVEL=`grep -i "maximum" $BASEDIR/tools/grabLaundry.sh |  awk -F' ' '{print $3}' | awk -F')' '{print $1}'`".0"

echo "selecting url from news where level > $MAXLEVEL; ..."


sqlite3 $DBDIR/urls.db -line "select url from news where level > $MAXLEVEL;" | sed -e "s/url =//g" 

echo "put that into followme.txt"
#we shoudl automate this

# should print out the username of anyone this fails with in a greppable way
# there's an issue in this with spc deal with spc users

echo "when done. press enter or something";
read ;

for x in `cat followme.txt`; do
echo $x;
toot follow $x; 
sleep $HOW_SLOW_IS_SPC;
done;


check_for_banned_from_spc.sh users.txt


echo "delete from news where level > $MAXLEVEL;"
